﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Data;

namespace AGUnitEditor.Windows
{
    public partial class AGSelectModelWindow : Form
    {
        List<AGModelCategory> _categoryList;
        List<AGModel> _modelList;

        public AGModel SelectedModel { get; private set; }

        public AGSelectModelWindow(List<AGModelCategory> categoryList, List<AGModel> modelList)
        {
            InitializeComponent();

            _categoryList = categoryList;
            _modelList = modelList;

            _ctlListCategory.DataSource = _categoryList;
        }

        private void _ctlBtnOK_Click(object sender, EventArgs e)
        {
            SelectedModel = _ctlListModel.SelectedItem as AGModel;
            if (SelectedModel == null)
            {
                return;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void _ctlListCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            AGModelCategory selCategory = _ctlListCategory.SelectedItem as AGModelCategory;

            if (selCategory == null)
            {
                return;
            }

            List<AGModel> modelList = new List<AGModel>();
            for (int index = 0; index < _modelList.Count; index++)
            {
                AGModel model = _modelList[index];
                if (model.CategoryId == selCategory.Id)
                {
                    modelList.Add(model);
                }
            }

            _ctlListModel.DataSource = modelList;
        }
    }
}
